{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch02/and_gate.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) -> 0\n",
      "(1, 0) -> 0\n",
      "(0, 1) -> 0\n",
      "(1, 1) -> 1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def AND(x1, x2):\n",
    "    x = np.array([x1, x2])\n",
    "    w = np.array([0.5, 0.5])\n",
    "    b = -0.7\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:\n",
    "        y = AND(xs[0], xs[1])\n",
    "        print(str(xs) + \" -> \" + str(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch02/nand_gate.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) -> 1\n",
      "(1, 0) -> 1\n",
      "(0, 1) -> 1\n",
      "(1, 1) -> 0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def NAND(x1, x2):\n",
    "    x = np.array([x1, x2])\n",
    "    w = np.array([-0.5, -0.5])\n",
    "    b = 0.7\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:\n",
    "        y = NAND(xs[0], xs[1])\n",
    "        print(str(xs) + \" -> \" + str(y))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch02/or_gate.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) -> 0\n",
      "(1, 0) -> 1\n",
      "(0, 1) -> 1\n",
      "(1, 1) -> 1\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "def OR(x1, x2):\n",
    "    x = np.array([x1, x2])\n",
    "    w = np.array([0.5, 0.5])\n",
    "    b = -0.2\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    else:\n",
    "        return 1\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:\n",
    "        y = OR(xs[0], xs[1])\n",
    "        print(str(xs) + \" -> \" + str(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ch02/xor_gate.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) -> 0\n",
      "(1, 0) -> 1\n",
      "(0, 1) -> 1\n",
      "(1, 1) -> 0\n"
     ]
    }
   ],
   "source": [
    "def XOR(x1, x2):\n",
    "    s1 = NAND(x1, x2)\n",
    "    s2 = OR(x1, x2)\n",
    "    y = AND(s1, s2)\n",
    "    return y\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    for xs in [(0, 0), (1, 0), (0, 1), (1, 1)]:\n",
    "        y = XOR(xs[0], xs[1])\n",
    "        print(str(xs) + \" -> \" + str(y))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dezero:Python",
   "language": "python",
   "name": "conda-env-dezero-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
